﻿
using BLL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Inventory.sontemplate
{
    public partial class _Category : Inventory.template.BaseInfoForm
    {
        private BLL.CategoryBLL b1;
        private Category category;
        private List<Category> list;
        public _Category()
        {
           
            InitializeComponent();
            b1 = new BLL.CategoryBLL();//实例化业务访问对象
            BindDataGridView();
        }

        private void tagbtnXz_Click(object sender, EventArgs e)
        {

        }
        private string Validation()
        {
            if (txtCplbbh .Text .Trim() == "")
            {
                txtCplbbh.Focus();//将光标定位至产品类型编号输入框
                return "产品类别编号不为空！";
            }
            if (txtCplbmc .Text.Trim() == "")
            {
                txtCplbbh.Focus();
                return "产品类别名称不为空！";
            }
            return "";

        }
        /// <summary>
        /// 表格数据绑定
        /// </summary>
     private void BindDataGridView()
        {
            dgvCp.DataSource = null;//清空当前表格中的数据
            list = b1.GetList();
            dgvCp.DataSource = list;//将查询结果绑定至控件dgvCp
        }


        private void tagbtnCp_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0)
                MessageBox.Show(errorString, "系统提示");
            else
            {
                //如果操作值为INSERT，则进行新增操作
                if (opera == Operation.INSERT)
                {
                    string cID = txtCplbbh.Text.Trim();//获取控件中categoryID值
                    string cName = txtCplbmc .Text.Trim();//获取空间中categoryName值
                    category = new Category(cID, cName);//调用Category有参构造函数进行传值和实例化
                    if (b1.Insert(category))
                    {
                        BindDataGridView();
                    }
                    MessageBox.Show("添加成功", "系统提示！");
                    Clear();
                }
                if (opera == Operation.UPDATE)
                {
                    string cID = txtCplbbh.Text.Trim();//获取控件中categoryID值
                    string cName = txtCplbmc.Text.Trim();//获取空间中categoryName值
                    category = new Category(cID, cName);//调用Category有参构造函数进行传值和实例
                    if (b1.Update(category))
                    {
                        BindDataGridView();
                    }
                    MessageBox.Show("修改成功", "系统提示！");
                    Clear();
                }
                if (opera == Operation.DELETE)
                {
                    string cID = txtCplbbh.Text.Trim();//获取控件中categoryID值
                    string cName = txtCplbmc.Text.Trim();//获取空间中categoryName值
                    category = new Category(cID, cName);//调用Category有参构造函数进行传值和实例
                    if (b1.Delete(category))
                    {
                        BindDataGridView();
                    }
                    MessageBox.Show("删除成功", "系统提示！");
                   
                }
            }
        }

        private void dgvCp_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string cID = dgvCp .CurrentRow.Cells["Cplbbh"].Value.ToString();
            category = b1.FindByID(cID);
            if (opera == Operation.UPDATE || opera == Operation.DELETE)
            {
                txtCplbbh .Text = category.CategoryID;
                txtCplbmc .Text = category.CategoryName;
            }
        }

        private void tagbtnXg_Click(object sender, EventArgs e)
        {
            txtCplbbh.Text = dgvCp.CurrentRow.Cells["Cplbbh"].Value.ToString();
            txtCplbmc.Text = dgvCp.CurrentRow.Cells["Cplbmc"].Value.ToString();
        }

        private void txtCplbbh_TextChanged(object sender, EventArgs e)
        {

        }

        private void tagbtnSc_Click(object sender, EventArgs e)
        {
            txtCplbbh.Text= dgvCp.CurrentRow.Cells["Cplbbh"].Value.ToString();
            txtCplbmc .Text = dgvCp.CurrentRow.Cells["Cplbmc"].Value.ToString();
        }
    }
}
